package com.Txunda.fxw.ui.activity.other;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.tool.Toolkit;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.http.FeedBack;
import com.Txunda.fxw.ui.toolkit.glide.GlideUtils;
import com.bumptech.glide.Glide;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 评价
 *
 * @author Administrator
 */
public class LeaveMessageAty extends BaseAty {

    @BindView(R.id.aty_title3_icon)
    ImageView atyTitle3Icon;
    @BindView(R.id.aty_title3_tv_center)
    TextView atyTitle3TvCenter;
    @BindView(R.id.aty_title3_tv_right2)
    TextView atyTitle3TvRight2;
    @BindView(R.id.edit_content)
    EditText editContent;
    @BindView(R.id.selected_pic_iv)
    ImageView selectedPicIv;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.user_leave_ev)
    EditText userLeaveEv;

    private FeedBack feedBack;
    private File head_pic;
    private int size = 0;
    private String m_id = "";
    private String type = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String pic = application.getUserInfo().get("head_pic");
        GlideUtils.urlCenterCrop(pic, size, size, selectedPicIv);
        String nickname = application.getUserInfo().get("nickname");
        userLeaveEv.setText(nickname);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_leave_message;
    }

    @Override
    protected void initialized() {
        atyTitle3TvCenter.setText("评价");
        feedBack = new FeedBack();
        size = Toolkit.dip2px(this, 120);
        type = getIntent().getStringExtra("type");
        m_id = application.getUserInfo().get("m_id");
        initImagePicker();
    }


    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("/Api/Feedback/addFeedback")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            showTips(R.drawable.icon_right_tip, MapUtils.isEmpty(map) ? "评价成功" : map.get("message"));
            Intent intent = getIntent();
            intent.putExtra("success", true);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    @OnClick({R.id.selected_pic_iv, R.id.tv_submit})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.selected_pic_iv:
                startActivityForResult(ImageGridActivity.class, null, 100);
                break;
            case R.id.tv_submit:
                String content = editContent.getText().toString();
                String nick = userLeaveEv.getText().toString();
                if (content.equals("")) {
                    showTips(R.drawable.error, "请输入要提交的内容");
                    break;
                }
                showProgressDialog();
                feedBack.addFeedback(content, m_id, type, head_pic, nick, this);
                break;

            default:
                break;
        }
    }

    @Override
    protected void requestData() {

    }

    ArrayList<ImageItem> images = null;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == 100) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                head_pic = new File(images.get(0).path);
                Glide.with(this)
                        .load(head_pic)
                        .centerCrop()
                        .override(size, size)
                        .into(selectedPicIv);
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
